package top.mcwebsite.playandroid.business.tutorial.impl.ui

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.Text
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import coil.compose.AsyncImage
import top.mcwebsite.lib.route.api.EasyRouter
import top.mcwebsite.playandroid.business.tutorial.api.TutorialConstant
import top.mcwebsite.playandroid.business.tutorial.api.model.TutorialModel
import top.mcwebsite.playandroid.business.tutorial.impl.vm.TutorialIntent
import top.mcwebsite.playandroid.business.tutorial.impl.vm.TutorialUiEffect
import top.mcwebsite.playandroid.business.tutorial.impl.vm.TutorialUiState
import top.mcwebsite.playandroid.business.tutorial.impl.vm.TutorialViewModel
import top.mcwebsite.playandroid.common.list.EasyLazyListPageColumn
import top.mcwebsite.playandroid.common.uiWidget.SkeletonPage
import top.mcwebsite.playandroid.common.uiWidget.skeleton
import top.mcwebsite.playandroid.strings.Str

@Composable
fun TutorialPage() {
    val viewModel: TutorialViewModel = viewModel()
    val uiState by viewModel.uiStateFlow.collectAsState()
    viewModel.HandleUiEffect {
        when (it) {
            is TutorialUiEffect.GoToTutorialMenuEffect -> {
                EasyRouter.navigate(TutorialConstant.TUTORIAL_MENU_RUTE) {
                    withParcelable("tutorial", it.tutorialModel)
                }
            }
        }
    }
    TutorialPage(uiState) {
        viewModel.sendIntent(it)
    }
}

@Composable
fun TutorialPage(
    uiState: TutorialUiState,
    sendEvent: (TutorialIntent) -> Unit,
) {
    EasyLazyListPageColumn(
        uiState.tutorialsAsync,
        loading = {
            SkeletonPage {
                TutorialSkeleton()
            }
        },
        retry = {

        }
    ) { _, item ->
        TutorialItem(item, sendEvent)
    }
}

@Composable
fun TutorialItem(
    tutorialModel: TutorialModel, sendEvent: (TutorialIntent) -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable {
                sendEvent(TutorialIntent.RequestToTutorialMenuIntent(tutorialModel))
            }
            .padding(vertical = 10.dp, horizontal = 16.dp)
    ) {
        AsyncImage(
            tutorialModel.cover,
            modifier = Modifier
                .width(86.dp)
                .height(120.dp),
            contentDescription = null,
        )
        Spacer(modifier = Modifier.width(15.dp))
        Column {
            Text(tutorialModel.name, style = MaterialTheme.typography.titleMedium)
            Spacer(modifier = Modifier.height(5.dp))
            Text(
                stringResource(id = Str.author_is, tutorialModel.author),
                style = MaterialTheme.typography.labelMedium,
                color = LocalContentColor.current.copy(alpha = 0.7f)
            )
            Spacer(modifier = Modifier.height(10.dp))
            Text(
                tutorialModel.desc,
                maxLines = 3,
                overflow = TextOverflow.Ellipsis,
                style = MaterialTheme.typography.labelMedium,
                color = LocalContentColor.current.copy(alpha = 0.7f)
            )
        }
    }
}

@Composable
fun TutorialSkeleton() {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 10.dp, horizontal = 16.dp)
    ) {
        Box(
            modifier = Modifier
                .width(86.dp)
                .height(120.dp)
                .skeleton(true),
        )
        Spacer(modifier = Modifier.width(15.dp))
        Column {
            Box(
                modifier = Modifier
                    .height(15.dp)
                    .fillMaxWidth()
                    .skeleton(true)
            )
            Spacer(modifier = Modifier.height(5.dp))
            Box(
                modifier = Modifier
                    .height(12.dp)
                    .fillMaxWidth(0.5f)
                    .skeleton(true)
            )
            Spacer(modifier = Modifier.height(10.dp))
            Box(
                modifier = Modifier
                    .height(12.dp)
                    .fillMaxWidth(1f)
                    .skeleton(true)
            )
            Spacer(modifier = Modifier.height(3.dp))
            Box(
                modifier = Modifier
                    .height(12.dp)
                    .fillMaxWidth(1f)
                    .skeleton(true)
            )
            Spacer(modifier = Modifier.height(3.dp))
            Box(
                modifier = Modifier
                    .height(12.dp)
                    .fillMaxWidth(0.5f)
                    .skeleton(true)
            )
        }
    }
}